package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.CommonFragmentPageAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonTab;
import net.caixiaomi.info.model.TeamDetailForDiscoveryEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by mac on 2018/9/11.
 */

public class TeamDetailActivity extends BaseActivity {

    @BindView(R.id.team_logo)
    ImageView mTeamLogo;

    @BindView(R.id.team_name)
    TextView mTeamName;

    @BindView(R.id.team_build_time)
    TextView mBuildTime;

    @BindView(R.id.team_court)
    TextView mTeamCourt;

    @BindView(R.id.team_country)
    TextView mTeamCountry;

    @BindView(R.id.team_capacity)
    TextView mTeamCapacity;

    @BindView(R.id.team_city)
    TextView mTeamCity;

    @BindView(R.id.team_value)
    TextView mTeamValue;

    private int teamId;
    private Context mContext;

    private CommonFragmentPageAdapter mAdapter;
    TeamPersonListFragment fragment0;
    TeamPersonRecordFragment fragment1;
    TeamPersonFutureFragment fragment2;
    @BindView(R.id.tab)
    TabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    @BindView(R.id.appbar)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;
    @BindView(R.id.container)
    View mContainer;
    @BindView(R.id.title)
    TextView mTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitle.setText("");
        mContainer.setVisibility(View.INVISIBLE);
        teamId=getIntent().getIntExtra("teamId",0);

        mContext=this;
        findViewById(R.id.frag_group).setFitsSystemWindows(true);

        String[] tabArray = getResources().getStringArray(R.array.TAB_TEAM_DETAIL);

        List<Fragment> data = new ArrayList<>();
        List<CommonTab> titles = new ArrayList<>();
        for (int index = 0; index < tabArray.length; index++) {
            CommonTab tab = new CommonTab(index, tabArray[index]);
            titles.add(tab);
            switch (index){
                case 0:
                    fragment0 = new TeamPersonListFragment();
                    break;
                case 1:
                    fragment1 = new TeamPersonRecordFragment();
                    break;
                case 2:
                    fragment2 = new TeamPersonFutureFragment();
                    break;
            }

        }
        data.add(fragment0);
        data.add(fragment1);
        data.add(fragment2);

        mAdapter = new CommonFragmentPageAdapter(getSupportFragmentManager(), titles, data);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.layout_divider_vertical));
//        mViewPager.addOnPageChangeListener(mChangeListener);
        mViewPager.setCurrentItem(0);

        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {//展开
                    mToolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {//折叠
                    mToolbar.setBackgroundColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.background));
                } else {//中间状态
                    mToolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                }
            }
        });

        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.team_detail_layout;
    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("teamId", teamId);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<TeamDetailForDiscoveryEntity>> dis = requestService.getTeamDetailForDiscovery(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<TeamDetailForDiscoveryEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<TeamDetailForDiscoveryEntity> data) {
                mContainer.setVisibility(View.VISIBLE);
                mProgress.setVisibility(View.GONE);
                bindData(data.data);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
            }
        });
    }

    private void bindData(TeamDetailForDiscoveryEntity entity){
        mTitle.setText(entity.getTeamAddr());
        mTeamName.setText(entity.getTeamAddr());
        mBuildTime.setText("成立时间: "+entity.getTeamTime());
        mTeamCourt.setText("球   场: "+entity.getCourt());
        mTeamCountry.setText("国家地区: "+entity.getContry());
        mTeamCapacity.setText("球场容量: "+entity.getTeamCapacity());
        mTeamCity.setText("所在城市: "+entity.getCity());
        mTeamValue.setText("球队价值: "+entity.getTeamValue());
        Glide.with(mTeamLogo.getContext()).load(entity.getTeamPic()).into(mTeamLogo);

        fragment0.initData(entity.getPlayerlist());
        fragment1.initData(entity.getRecentRecord());
        fragment2.initData(entity.getFutureMatch());
    }
}
